package com.egg.auth.service.dept.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egg.auth.entity.dept.DeptLinkUser;
import com.egg.auth.mapper.dept.DeptLinkUserMapper;
import com.egg.auth.service.dept.IDeptLinkUserService;
import com.egg.common.mybatis.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


@Slf4j
@Service
public class DeptLinkUserServiceImpl extends BaseServiceImpl<DeptLinkUserMapper, DeptLinkUser> implements IDeptLinkUserService {


    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void put(Serializable userId, Collection<? extends Serializable> deptId) {
        delByUserId(userId);
        if (ObjectUtil.isEmpty(deptId)) {
            return;
        }
        List<DeptLinkUser> deptLinkUserList = new ArrayList<>();
        for (Serializable temp : deptId) {
            DeptLinkUser deptLinkUser = new DeptLinkUser();
            deptLinkUser.setUserId(Long.parseLong(userId.toString()));
            deptLinkUser.setDeptId(Long.parseLong(temp.toString()));
            deptLinkUserList.add(deptLinkUser);
        }
        baseSave(deptLinkUserList);
    }

    @Override
    public List<DeptLinkUser> findByDeptId(Collection<? extends Serializable> deptId) {
        if (ObjectUtil.isEmpty(deptId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<DeptLinkUser> qw = buildLambdaSortQw();
        qw.in(DeptLinkUser::getDeptId, deptId);
        return list(qw);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delByDeptId(Collection<? extends Serializable> deptId) {
        if (ObjectUtil.isEmpty(deptId)) {
            return;
        }
        LambdaQueryWrapper<DeptLinkUser> qw = buildLambdaQw();
        qw.in(DeptLinkUser::getDeptId, deptId);
        remove(qw);
    }

    @Override
    public List<DeptLinkUser> findByUserId(Serializable userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return Collections.emptyList();
        }
        return findByUserId(Arrays.asList(userId));
    }

    @Override
    public List<DeptLinkUser> findByUserId(Collection<? extends Serializable> userIds) {
        if (ObjectUtil.isEmpty(userIds)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<DeptLinkUser> qw = buildLambdaSortQw();
        qw.in(DeptLinkUser::getUserId, userIds);
        return list(qw);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delByUserId(Serializable userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return;
        }
        delByUserId(Arrays.asList(userId));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delByUserId(Collection<? extends Serializable> userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return;
        }
        LambdaQueryWrapper<DeptLinkUser> qw = buildLambdaQw();
        qw.in(DeptLinkUser::getUserId, userId);
        remove(qw);
    }

}
